package kr.ac.ml.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.BufferedReader;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import kr.ac.ml.action.ConfirmAction;
import kr.ac.ml.action.ErrorAction;
import kr.ac.ml.action.JSonAction;
import kr.ac.ml.action.MainAction;
import kr.ac.ml.action.MakeFriendTable;
import kr.ac.ml.mvc.Action;

/**
 * Servlet implementation class Jsonservlet
 */
@WebServlet("/json")
public class Jsonservlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Jsonservlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

	
		
		String a = request.getParameter("a");
		String content = request.getParameter("content");
		Action action = null;

		System.out.println("mainservlet");
		System.out.println("a : " + a);
		System.out.println("content : " + content);

		if ("confirm".equals(a)) {
			action = new ConfirmAction();
		} else if ("transform".equals(a)) {
			action = new JSonAction();
		}else
			action = new ErrorAction();
		action.execute(request, response);

		System.out.println(request.getAttribute("json"));
		response.setContentType("text/html; charset=utf-8");

		PrintWriter out = response.getWriter();
		out.print(request.getAttribute("json"));
	}
}
